package com.chhlz.module.base.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.chhlz.core.action.BaseAction;
import com.chhlz.core.action.Json;
import com.chhlz.core.util.ExceptionUtil;
import com.chhlz.module.service.BaseService;

public class BasicDataAction<T> extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6251902440251267756L;
	protected static final Logger LOG = Logger.getLogger(BasicDataAction.class);
	
	protected void grid(BaseService<T> baseService){
		int total = baseService.getTotal();
		List<T> list = baseService.findAll(total, total, sort, order);
		this.writeJson(total, list);
	}
	
	protected void add(BaseService<T> baseService, T t){
		Json result = new Json();
		try {
			baseService.save(t);
			result.setSuccess(true);
			result.setMsg("添加成功");					
		} catch (Exception e) {
			LOG.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加失败");					
		}
		writeJson(result);
	}
	
	protected void edit(BaseService<T> baseService, T t) {
		Json result = new Json();
		try {
			baseService.update(t);
			result.setSuccess(true);
			result.setMsg("修改成功");
			result.setSuccess(true);
		} catch (Exception e) {
			LOG.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("添加失败");
		}
		writeJson(result);
	}
	
	public void remove(BaseService<T> baseService, int id) {
		Json result = new Json();
		try {
			baseService.delete(id);
			result.setSuccess(true);
			result.setMsg("删除成功");
		} catch (Exception e) {
			LOG.debug(ExceptionUtil.getExceptionMessage(e));
			result.setMsg("删除失败");
		}
		writeJson(result);
	}

}
